package cn.tedu.zhyl.iot.controller;

import cn.tedu.zhyl.base.response.JsonResult;
import cn.tedu.zhyl.iot.pojo.dto.DeviceDto;
import cn.tedu.zhyl.iot.pojo.entity.Device;
import cn.tedu.zhyl.iot.service.IotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/iot/")
public class IotController {
    @Autowired
    private IotService iotService;

    /**
     * 查看产品列表
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("get/productList/{page}/{limit}")
    public JsonResult getProductList(@PathVariable Integer page, @PathVariable Integer limit){
        String productList = iotService.getProductList(page,limit);
        return JsonResult.ok(productList);
    }

    /**
     * 查看产品下面的设备
     * @param productId
     * @return
     */
    @GetMapping("get/deviceList/{productId}")
    public JsonResult<List<Device>> getDeviceList(@PathVariable("productId") String productId){
        List<Device> deviceList = iotService.getDeviceList(productId);
        return JsonResult.ok(deviceList);
    }

    /**
     * 查看设备详情
     * @param productId
     * @param deviceName
     * @return
     * @throws Exception
     */
    @GetMapping("get/deviceDetail/{productId}/{deviceName}")
    public JsonResult<Device> getDeviceDetail(@PathVariable("productId") String productId,@PathVariable("deviceName")String deviceName) throws Exception {
        Device deviceInfo =  iotService.getDeviceDetail(productId,deviceName);
        return JsonResult.ok(deviceInfo);
    }

    /**
     * 查看设备状态
     * @param productId
     * @param deviceName
     * @return
     * @throws Exception
     */
    @GetMapping("get/deviceStatus/{productId}/{deviceName}")
    public JsonResult<String> getDeviceStatus(@PathVariable("productId") String productId,@PathVariable("deviceName")String deviceName) throws Exception {
       String info = iotService.getDeviceStatus(productId,deviceName);
        return JsonResult.ok(info);
    }
    @GetMapping("get/getThingModelData/{productId}/{deviceName}")
    public JsonResult<String> getThingModelData(@PathVariable("productId") String productId,@PathVariable("deviceName")String deviceName) throws Exception {
        String dataJson = iotService.getThingModelData(productId,deviceName);
        return JsonResult.ok(dataJson);
    }
    /**
     * 删除设备
     * @param pk
     * @param deviceName
     * @return
     * @throws Exception
     */

    @GetMapping("del/device/{pk}/{deviceName}")
    public JsonResult delDevice(@PathVariable("pk")String pk,
                                @PathVariable("deviceName") String deviceName) throws Exception {
        iotService.delDevice(pk,deviceName);
        return JsonResult.ok();
    }

    /**
     * 添加设备
     * @param dto
     * @return
     * @throws Exception
     */
    @PostMapping("add/device")
    public JsonResult registerDevice(DeviceDto dto) throws Exception {
        iotService.register(dto);
        return JsonResult.ok();
    }

//    IotController@Autowired
//    @PostMapping("registerDevice")
//    public JsonResult registerDevice(DeviceDto dto){
//    }
}
